package cn.hh.basic.service.impl;

import cn.hh.basic.mapper.BaseMapper;
import cn.hh.basic.query.BaseQuery;
import cn.hh.basic.service.BaseService;
import cn.hh.basic.util.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  业务事项类: 员工
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS) // 放在类上给所有查询方法开启事务 (只读)
// @Inherited - 表示当前注解可以被子类继承
// @Service - 注解不能被继承
public class BaseServiceImpl<T> implements BaseService<T> { // 有泛型一定要加泛型  BaseServiceImpl<T>  BaseService<T>

    @Autowired  // 注入Mapper接口
    private BaseMapper<T> baseMapper ;

    // 查询所有
    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    // 查询单个
    @Override
    public T findOne(Long id) {
        return baseMapper.loadById(id);
    }

    // 添加操作
    @Transactional // 添加开启事务
    @Override
    public void add(T t) {
        baseMapper.save(t);
    }

    // 删除操作
    @Transactional // 删除开启事务
    @Override
    public void del(Long id) {
        baseMapper.remove(id);
    }

    // 修改操作
    @Transactional // 修改开启事务
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }


    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //先查询总数量
        Long totals = baseMapper.queryCount(query);
        if(totals < 1){
            return new PageList<T>();
        }
        //再查询当前页的分页数据
        List<T> data = baseMapper.queryData(query);
        return new PageList<T>(totals,data);
    }

    // 批量删除
    @Override
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }
}
